<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML表格与表单标签示例</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: Arial, sans-serif;
        }

        body {
            max-width: 1200px;
            margin: 20px auto;
            padding: 0 20px;
        }

        h1, h2 {
            color: #333;
            margin-bottom: 15px;
        }

        h2 {
            font-size: 1.2em;
            border-left: 3px solid #0066cc;
            padding-left: 10px;
        }

        table {
            border-collapse: collapse;
            width: 60%;
            margin: 20px 0;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
        }

        th, td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: center;
        }

        th {
            background-color: #f2f2f2;
            font-weight: 600;
        }

        caption {
            font-size: 1.1em;
            margin-bottom: 10px;
            color: #0066cc;
            font-weight: 500;
        }

        tfoot td {
            background-color: #f8f8f8;
            font-weight: 500;
        }

        fieldset {
            width: 50%;
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 0 3px rgba(0, 0, 0, 0.05);
        }

        legend {
            color: #0066cc;
            font-weight: 500;
            padding: 0 10px;
        }

        label {
            display: inline-block;
            width: 120px;
            margin-bottom: 10px;
            color: #333;
        }

        input, select, textarea {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 3px;
            width: 250px;
            margin-bottom: 10px;
        }

        textarea {
            width: 300px;
            resize: vertical;
        }

        input[type="file"] {
            border: none;
            padding: 0;
        }

        button {
            padding: 8px 15px;
            margin-right: 10px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        button[type="submit"] {
            background-color: #0066cc;
            color: white;
        }

        button[type="reset"] {
            background-color: #f2f2f2;
            color: #333;
        }

        button[type="button"] {
            background-color: #4CAF50;
            color: white;
        }

        hr {
            border: none;
            border-top: 1px solid #ddd;
            margin: 20px 0;
        }
    </style>
</head>

<body>

    <h1>表格与表单标签实验</h1>

    <!-- 表格部分 -->
    <h2>1. 学生信息表格</h2>
    <table>
        <caption>计科A2352班学生信息表</caption>
        <thead>
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>专业</th>
                <th>成绩</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>20230201111</td>
                <td>陈一</td>
                <td>男</td>
                <td>软件工程</td>
                <td>98</td>
            </tr>
            <tr>
                <td>20230202222</td>
                <td>王二</td>
                <td>女</td>
                <td>软件工程</td>
                <td>95</td>
            </tr>
            <tr>
                <td>20230203333</td>
                <td>张三</td>
                <td>男</td>
                <td>软件工程</td>
                <td>95</td>
            </tr>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="5">汇总：共3名学生，平均成绩96分</td>
            </tr>
        </tfoot>
    </table>
    <hr>

    <!-- 表单部分 -->
    <h2>2. 用户注册表单</h2>
    <form action="#" method="POST">
        <fieldset>
            <legend>基本信息</legend>
            <label for="user-name">用户名：</label>
            <input type="text" id="user-name" name="user-name" required><br>

            <label for="user-pwd">密码：</label>
            <input type="password" id="user-pwd" name="user-pwd" required><br>

            <label for="user-email">邮箱：</label>
            <input type="email" id="user-email" name="user-email" required><br>

            <label for="user-birth">出生日期：</label>
            <input type="date" id="user-birth" name="user-birth"><br>
        </fieldset>
        <fieldset>
            <legend>附加信息</legend>
            <label for="user-avatar">上传头像：</label>
            <input type="file" id="user-avatar" name="user-avatar" accept="image/*"><br>

            <label for="country">国家：</label>
            <select id="country" name="country">
                <option value="">--请选择--</option>
                <option value="china">中国</option>
                <option value="usa">美国</option>
            </select><br>

            <label>性别：</label>
            <input type="radio" id="gender-male" name="gender" value="male" checked>
            <label for="gender-male" style="width: auto;">男</label>
            <input type="radio" id="gender-female" name="gender" value="female">
            <label for="gender-female" style="width: auto;">女</label><br>

            <label>兴趣爱好：</label>
            <input type="checkbox" id="hobby-coding" name="hobby" value="coding">
            <label for="hobby-coding" style="width: auto;">打球</label>
            <input type="checkbox" id="hobby-reading" name="hobby" value="reading">
            <label for="hobby-reading" style="width: auto;">阅读</label>
            <input type="checkbox" id="hobby-sports" name="hobby" value="sports">
            <label for="hobby-sports" style="width: auto;">跑步</label><br>

            <label for="message">个人简介：</label>
            <textarea id="message" name="message" rows="4" cols="40"></textarea><br>

            <button type="submit">提交注册</button>
            <button type="reset">重置表单</button>
            <button type="button" onclick="previewUsername()">预览用户名</button>
        </fieldset>
    </form>

    <hr>
    <h3>实验小结</h3>
    <p><strong>表格标签</strong>：通过&lt;thead&gt;、&lt;tbody&gt;、&lt;tfoot&gt;语义化拆分，结合&lt;caption&gt;（标题）、&lt;th&gt;（表头）、colspan（合并单元格），实现数据结构化展示与汇总。</p>
    <p><strong>表单标签</strong>：利用&lt;fieldset&gt;分组，搭配多种input类型（text、password、email、date、file）、select、textarea，结合label关联与按钮功能，完成用户信息交互收集，method="POST"保障数据安全。</p>

    <script>
        function previewUsername() {
            const username = document.getElementById('user-name').value;
            alert(username ? `用户名：${username}` : '请先输入用户名');
        }
    </script>
</body>

</html>